//
//  OrderViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/22.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SVProgressHUD
import SwiftyJSON

class OrderViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    
    var dataArray = NSMutableArray()
    var myTableView = UITableView(frame: CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64), style: .Plain)
    let ORDERCELL = "orderCell"
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.creatNavView()
        self.getData()
        self.creatTableView()
    }
    
    
}


extension OrderViewController{
    
    private func creatNavView(){
        self.view.backgroundColor = UIColor.whiteColor()
        self.automaticallyAdjustsScrollViewInsets = false
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 94/255.0, green: 200/255.0, blue: 252/255.0, alpha: 1)
        
        self.title = "ORDER"
        
        let leftBtn = UIButton(type: .Custom)
        leftBtn.setImage(UIImage(named: "nav_back"), forState: .Normal)
        leftBtn.frame = CGRectMake(0, 0, 30, 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), forControlEvents: .TouchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
    }
    
    func leftBtnClick(){
        self.dismissViewControllerAnimated(true) {
            
        }
    }
}
extension OrderViewController{
    private func getData(){
        
        if UserSession.shareInstance.isLogin == true {
            
            let urlStr = APIBASE + "main_page=account_history"
            
            let parma = ["zenid":UserSession.shareInstance.cookie!,"current_page_number":"1"]
            NetworkTool.shareInstance.request(.POST, urlString: urlStr, parmas: parma, finishedBlock: { (result, error) in
                if result != nil{
//                    let json = JSON(result!)
//                    print(json)
                    
                    let arr = ((result as? NSDictionary)!["data"]as?NSDictionary)!["order_history"]as?[[String:NSObject]]
                    for dic in arr!{
                        let model = OderModel(dictionary: dic)
                        self.dataArray.addObject(model)
                    }
                    
                    self.myTableView.reloadData()
                    
                }else{
                    SVProgressHUD.showErrorWithStatus("get orderData error:" + String(error))
                    print("get orderData error:" + String(error))
                }
            })
            
            
        }
    }
}

extension OrderViewController{
    private func creatTableView(){
        
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)
        let nib = UINib(nibName: "OrderTableViewCell", bundle: nil)
        myTableView.registerNib(nib, forCellReuseIdentifier: ORDERCELL)
        
        
    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ORDERCELL)
        let model = self.dataArray[indexPath.row]as?OderModel
        (cell as? OrderTableViewCell)?.model = model
        cell?.selectionStyle = .None
        return cell!
        
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return SCREEN_HEIGHT*0.2
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let model = self.dataArray[indexPath.row]as?OderModel
        let orderDetailVC = OrderDetailViewController()
        orderDetailVC.orderID = model?.orderNumber
        
        self.navigationController?.pushViewController(orderDetailVC, animated: true)
        
        
        
    }
}














